

import { WeaLocaleProvider, WeaTools } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import { getLunarDatas } from './util.js';
import { formatDate, NoRightCom } from '../../util/common';
/**
  * 周/月历控件
  * @param container {string} 必选,容器选择器
  * @param options.defaultDate {date}  可选,默认为当前年月日
  * @param options.clickDate {function} 点击日期的回调，携带参数为对象形式,包含年月日,字符串年月日
  * @param options.clickPreNextDate {function} 点击切换箭头的回调，携带参数为对象形式,包含年月日,字符串年月日
  * @param options.getSelectedDate {function} 获取选中的值,默认获取当天日期,携带参数为对象形式,包含年月日
  * @param options.getDateType {string} 获取日历视图类型，周，双周，1日/2周/3月/4双周
  * @param options.getWeekstartday 当前日历元素以周一还是周日开头，0周日/1周一
  * @param options.getisShowLunarDatas  当前日历元素是否显示农历，0不显示/1显示
  * @param options.geteid 当前日历元素的eid
*/
const weeklyCalendar = function (container, options) {
  var options = options || {};

  /* 简化选择器操作 */
  let $$ = function (selector) {
    return document.querySelector(`${container} [role=${selector}]`);
  };

  /* 日期月份补零 */
  let zeroize = function (n) {
    let r = (n < 10 ? `0${n}` : n);
    return r;
  };

  // 为了获得每个月的日期有多少，我们需要判断 平年闰年[四年一闰，百年不闰，四百年再闰]
  let isLeapYear = year => (year % 400 === 0) || (year % 100 !== 0 && year % 4 === 0);

  // 获得每个月的日期有多少，注意 month - [0-11]
  const getMonthCount = (year, month) => {
    let arr = [
      31, null, 31, 30,
      31, 30, 31, 31,
      30, 31, 30, 31,
    ];
    let count = arr[month] || (isLeapYear(year) ? 29 : 28);
    return Array.from(new Array(count), (item, value) => value + 1);
  };

  // 获得某年某月的 1号 是星期几，这里要注意的是 JS 的 API-getDay() 是从 [日-六](0-6)，返回 number
  const getWeekday = (year, month) => {
    let date = new Date(year, month, 1);
    return date.getDay();
  };

   // 获得上个月的天数
   const getPreMonthCount = (year, month) => {
    if (month === 0) {
      return getMonthCount(year - 1, 11);
    }
      return getMonthCount(year, month - 1);
  };

  // 获得下个月的天数
  const getNextMonthCount = (year, month) => {
    if (month === 11) {
      return getMonthCount(year + 1, 0);
    }
      return getMonthCount(year, month + 1);
  };
  /* 点击选择日期（点击日期与左右切换箭头） */
  let selectedYear = null,
      selectedMonth = null,
      selectedDate = null,
      selectedDateTime = null;

  // 设置选中日期/当前日期 获取年月日及星期几,当前月天数
  let d = options.defaultDate ? new Date(options.defaultDate) : new Date();
  let activeDay = d.getDay(),
      activeDate = zeroize(d.getDate()),
      activeMonth = zeroize(d.getMonth() + 1),
      activeYear = d.getFullYear();

  // DateType当前视图
  let DateType = options.getDateType;
  let weektype = 7;
  // Weekstartday周几开始
  let Weekstartday = options.getWeekstartday;
  if(Weekstartday==1){
    if(activeDay!=0){
      activeDay=activeDay-1;
    }else{
      activeDay=6;
    }
  }
  // 当前元素的eid
  let eid = options.geteid;
  // 是否显示农历
  let isShowLunar = options.getisShowLunarDatas;
  if (DateType == '2') {
    weektype = 7;
  } else if (DateType == '4') {
    weektype = 14;
  } else if (DateType == '3') {

  }

  // 返回带有指定标签名的对象的集合document.querySelector('#portal_weeklyCalendar [role=weeklyCanlendarView]'),显示（非后台设置页面），后台设置页面
  let lis = $$('weeklyCanlendarView').getElementsByTagName('li'),
      aTags = $$('weeklyCanlendarView').getElementsByTagName('a');

  /* 创建星期，表头，周几 */
  let creatWeek = (function () {
    let span = '';
    let weeks_ch = Weekstartday == 0 ?
      [`${getLabel(523945, '日')}`, `${getLabel(523946, '一')}`, `${getLabel(523947, '二')}`, `${getLabel(523948, '三')}`, `${getLabel(523949, '四')}`, `${getLabel(523950, '五')}`, `${getLabel(523951, '六')}`] :
      [`${getLabel(523946, '一')}`, `${getLabel(523947, '二')}`, `${getLabel(523948, '三')}`, `${getLabel(523949, '四')}`, `${getLabel(523950, '五')}`, `${getLabel(523951, '六')}`, `${getLabel(523945, '日')}`];
    for (let i = 0, len = weeks_ch.length; i < len; i++) {
      Weekstartday == 0 ?
        span += `<li class="week_name_${i}">${weeks_ch[i]}</li>` :
        span += `<li class="week_name_${(i + 1) % 7}">${weeks_ch[i]}</li>`;
    }
    $$('weeks_ch').innerHTML = span;
  }());

  /* 动态设置周历表头的年月 */
  let setYearMonth = function (n) {
    let _d = new Date();
    let currDate = zeroize(_d.getDate()),
      currMonth = zeroize(_d.getMonth() + 1),
      currYear = parseInt(_d.getFullYear());
      // n为0 当天，n=1,左右切换周 处理表头显示年月不正确问题
       for(let i=0;i<lis.length;i++){
        if(lis[i].className=='clickActive'||lis[i].className=='clickActive active'){ 
          $$('year_selector').innerHTML = zeroize(parseInt(aTags[i].dataset.year));
          $$('month_selector').innerHTML = parseInt(aTags[i].dataset.month);
          $$('month_selector').innerHTML;
        }
       }
  };

  /* 动态设置月历表头的年月 */
  let setMonthCalendarYearMonth = function (MonthCalendarMonth, MonthCalendarYear) {
    $$('year_selector').innerHTML = MonthCalendarYear;
    $$('month_selector').innerHTML = parseInt(MonthCalendarMonth);
  };

  /**
    * 周历使用
    * 计算过去或者是未来时间(7天，周单位)
    * @param {return} obj 返回的月份和日期
    * @param {number} num 过去或者是未来的某天
  */
  let calcTime = function (num) {
    var num = num || 0,
      someTime = d.getTime() + (24 * 60 * 60 * 1000) * num,
      someYear = new Date(someTime).getFullYear(),
      someMonth = zeroize(new Date(someTime).getMonth() + 1), // 未来月
      someDate = zeroize(new Date(someTime).getDate()), // 未来天
      someday = new Date(someTime).getDay();
    let obj = {
      year: someYear,
      month: someMonth,
      date: someDate,
      day: someday,
    };
    return obj;
  };

  /**
   * 创建月历
   *
  */
  let creatMonthCalendar = function (MonthCalendarMonth, MonthCalendarYear, numstatu) {
    // 生成日历数据，上个月剩下的的 x 天 + 当月的 28（平年的2月）或者29（闰年的2月）或者30或者31天 + 下个月的 y 天 = 42，res天数组
    let res = [];
    let currentMonth = getMonthCount(MonthCalendarYear, parseInt(MonthCalendarMonth) - 1);
    let preMonth = getPreMonthCount(MonthCalendarYear, parseInt(MonthCalendarMonth) - 1);
    let nextMonth = getNextMonthCount(MonthCalendarYear, parseInt(MonthCalendarMonth) - 1);
    let newwhereMonday=getWeekday(MonthCalendarYear, parseInt(MonthCalendarMonth) - 1) - 1 ==-1?6:getWeekday(MonthCalendarYear, parseInt(MonthCalendarMonth) - 1) - 1;
    let whereMonday = Weekstartday == 0 ? getWeekday(MonthCalendarYear, parseInt(MonthCalendarMonth) - 1) : newwhereMonday;
    if (whereMonday === 0) {
      whereMonday = 7;
    }
    let preArr = preMonth.slice(-1 * whereMonday);
    let nextArr = nextMonth.slice(0, 42 - currentMonth.length - whereMonday);
    res = [].concat(preArr, currentMonth, nextArr);

    // 渲染的html文本
    let a = '';

    // 获取当天
    let _d = new Date();
    let currDate = zeroize(_d.getDate()),
    currMonth = zeroize(_d.getMonth() + 1),
    currYear = _d.getFullYear();

    // 判断当前显示是否本月
    let isConMonth = false;
    if (MonthCalendarYear === currYear && parseInt(MonthCalendarMonth) === parseInt(currMonth)) {
      isConMonth = true;
    }

    // 遍历渲染结构
    for (let i = 0; i < res.length; i++) {
      let monthCal_curyear = MonthCalendarYear;
      let monthCal_curmonth = parseInt(MonthCalendarMonth);
      // 点击左右切换箭头重新定位日程内容，非当前月，选中当月第一天
      selectedYear = monthCal_curyear,
      selectedMonth = zeroize(monthCal_curmonth),
      // 没有切换过（初始），加载接口传回的日期，切换过，是否本月，是则加载本天，否则加载第一天
      selectedDate = numstatu == 1 ? activeDate : isConMonth ? currDate : '01';
      selectedDateTime = `${selectedYear}-${selectedMonth}-${selectedDate}`;

      if (i < preArr.length) { // 上个月
        monthCal_curmonth = parseInt(MonthCalendarMonth) - 1 < 1 ? 12 : parseInt(MonthCalendarMonth) - 1;
        monthCal_curyear = parseInt(MonthCalendarMonth) - 1 < 1 ? MonthCalendarYear - 1 : MonthCalendarYear;
      } else if (i > res.length - nextArr.length - 1) { // 下个月
        monthCal_curmonth = parseInt(MonthCalendarMonth) + 1 > 12 ? 1 : parseInt(MonthCalendarMonth) + 1;
        monthCal_curyear = parseInt(MonthCalendarMonth) + 1 > 12 ? MonthCalendarYear + 1 : MonthCalendarYear;
      }

      // 获取农历
      let lunarStr = getLunarDatas().toLunar(`${monthCal_curyear}`, parseInt(monthCal_curmonth), `${res[i]}`);
      let dataStr2 = getLunarDatas().toLunarMonth(lunarStr.lunarMonth) + getLunarDatas().toLunarNumber(lunarStr.lunarDay);
      let dataStr3 = parseInt(zeroize(monthCal_curmonth)) + zeroize(res[i]);
      let lunarDatas = lunarStr.lunarDay;
      if (lunarDatas == '初一') {
        lunarDatas = `${lunarStr.lunarMonth}月`;
      }
      // 获取农历节气
      let jq = getLunarDatas().getjq(`${monthCal_curyear}`, parseInt(zeroize(monthCal_curmonth)), zeroize(res[i]));
      if (jq != '') { lunarDatas = jq; }

      if (getLunarDatas().holidayName(`${monthCal_curyear}`, dataStr2, dataStr3) !== undefined) {
        lunarDatas = getLunarDatas().holidayName(`${monthCal_curyear}`, dataStr2, dataStr3);
      }
      let lunarDatasShow = '';
      let lunarDatasShowtitle = '';
      // isShowLunar判断是否显示农历
      isShowLunar == '1' ? lunarDatasShow = `<br/>${lunarDatas}` : '';
      isShowLunar == '1' ? lunarDatasShowtitle = `${lunarDatas}` : '';
      if (isConMonth && monthCal_curyear === currYear && monthCal_curmonth === parseInt(currMonth) && res[i] === parseInt(currDate) && monthCal_curyear === activeYear && monthCal_curmonth === parseInt(activeMonth) && res[i] === parseInt(activeDate)) {
        // 当前选中日期&当前日期同一天&&当月
        a += `<li class="clickActive active" data-role="active">
          <a  class="week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}  ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a> 
          </li>`;
      } else if (numstatu == 1 && monthCal_curyear === activeYear && monthCal_curmonth === parseInt(activeMonth) && res[i] === parseInt(activeDate)) {
        // 未切换箭头显示当前选中日期
        a += `<li class="clickActive"><a  class="week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}  ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a></li>`;
      } else if (numstatu == 0 && isConMonth && monthCal_curyear === currYear && monthCal_curmonth === parseInt(currMonth) && res[i] === parseInt(currDate)) {
        // 切换箭头，且包含当天，当月
        a += `<li class="clickActive active" data-role="active"><a  class="week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])} ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a></li>`;
      } else if (isConMonth == false && numstatu == 0 && monthCal_curyear === selectedYear && monthCal_curmonth === parseInt(selectedMonth) && res[i] === parseInt(selectedDate)) {
        // 切换箭头，且不包含当月，重定位
        a += `<li class="clickActive"><a  class="week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])} ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a></li>`;
      } else if (monthCal_curyear === currYear && monthCal_curmonth === parseInt(currMonth) && res[i] === parseInt(currDate) ){
        // 当天
        a += `<li class="active" data-role="active"><a  class="week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])} ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a></li>`;
      } else if (monthCal_curmonth !== parseInt(MonthCalendarMonth)) {
        // 非本月
        a += `<li class=""><a  class="othermonth week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])} ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a></li>`;
      } else {
        // 本月
        a += `<li  class=""><a  class="currentmonth week_name_${Weekstartday == 0 ? i % 7 : (i + 1) % 7}"  data-year="${monthCal_curyear}" data-month="${zeroize(monthCal_curmonth)}" data-date="${zeroize(res[i])}" data-time="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])}" title="${monthCal_curyear}-${zeroize(monthCal_curmonth)}-${zeroize(res[i])} ${lunarDatasShowtitle}">${zeroize(res[i])}${lunarDatasShow}</a></li>`;
      }
     }
    $$('weeklyCanlendarView').innerHTML = a;
  };

  /**
   * 创建周历
   * @param some {Number} 星期几  一周开始的角标，当天为0，过去-1，未来+1
  */
 let creatWeekCalendar = function (some, numstatu) {
  // 获取当天日期
  let _d = new Date();
  let currDate = zeroize(_d.getDate()),
    currMonth = zeroize(_d.getMonth() + 1),
    currYear = _d.getFullYear();

  // 渲染的html文本
  let a = '';

  // 判断当前显示日期中是否包含今天
  let isConToday = false;
  for (let i =some , len = some + weektype ; i < len; i++) {
    if (calcTime(i).year === currYear && calcTime(i).month === currMonth && calcTime(i).date === currDate) {
      isConToday = true;
    }
  }

  // 获取日历（阳历/农历）信息，渲染文本结构，Weekstartday判断周日还是周一作为开始
  for (let i = some, len =some + weektype ; i < len; i++) {
    // 农历数据
    let lunarStr = getLunarDatas().toLunar(`${calcTime(i).year}`, parseInt(calcTime(i).month), `${calcTime(i).date}`);
    let dataStr2 = getLunarDatas().toLunarMonth(lunarStr.lunarMonth) + getLunarDatas().toLunarNumber(lunarStr.lunarDay);
    let dataStr3 = parseInt(calcTime(i).month) + calcTime(i).date;
    let lunarDatas = lunarStr.lunarDay;
    if (lunarDatas == '初一') {
        lunarDatas = `${lunarStr.lunarMonth}月`;
    }
    // 获取农历节气
    let jq = getLunarDatas().getjq(`${calcTime(i).year}`, parseInt(calcTime(i).month), calcTime(i).date);
    if (jq != '') { lunarDatas = jq; }

    if (getLunarDatas().holidayName(`${calcTime(i).year}`, dataStr2, dataStr3) !== undefined) {
        lunarDatas = getLunarDatas().holidayName(`${calcTime(i).year}`, dataStr2, dataStr3);
    }
    let lunarDatasShow = '';
    let lunarDatasShowtitle = '';
    // isShowLunar判断是否显示农历
    isShowLunar == '1' ? lunarDatasShow = `<br/>${lunarDatas}` : '';
    isShowLunar == '1' ? lunarDatasShowtitle = `${lunarDatas}` : '';
    // 箭头切换，重新定位日期
  
    if (isConToday) { // 判断当前面板是否包含今天，是的话显示今天日程
          selectedDate = currDate;
          selectedMonth = currMonth;
    } else { // 判断当前面板是否包含今天，不包含则显示第一天
          selectedDate = calcTime(some).date;
          selectedMonth = calcTime(some).month;
    }
    selectedYear = calcTime(some).year,
        // selectedMonth = calcTime(some).month,
    selectedDateTime = `${selectedYear}-${selectedMonth}-${selectedDate}`;

    // 当天日期   calcTime(i).day获取是周几
    if (calcTime(i).year === currYear && calcTime(i).month === currMonth && calcTime(i).date === currDate && calcTime(i).year === activeYear && calcTime(i).month === activeMonth && calcTime(i).date === activeDate) {
        // 当前选中日期&当前日期同一天
        a += `<li class="clickActive active" data-role="active">        
        <a  class="week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}" data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    } else if (numstatu == 1 && calcTime(i).year === activeYear && calcTime(i).month === activeMonth && calcTime(i).date === activeDate) {
        // 当前选中的日期，numstatu==1，未切换过箭头则显示选中的日期
        a += `<li class="clickActive">
        <a  class="week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}" data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    } else if (numstatu == 0 && calcTime(i).year === currYear && calcTime(i).month === currMonth && calcTime(i).date === currDate) {
        // 当前选中的日期，numstatu==0，切换过箭头且包含当天
        a += `<li class="clickActive active" data-role="active">
        <a  class="week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}" data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    } else if (calcTime(i).year === selectedYear && calcTime(i).month === selectedMonth && calcTime(i).date === selectedDate && isConToday == false && numstatu == 0) {
        // 点击箭头切换,且不包含当天重定位
        a += `<li class="clickActive">
        <a  class="week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}" data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    } else if (calcTime(i).year === currYear && calcTime(i).month === currMonth && calcTime(i).date === currDate) {
        // 当天
        a += `<li class="active" data-role="active">
        <a  class="week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}"  data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    } else if (calcTime(i).month != calcTime(some).month) {
        // 非本月
        a += `<li  class="">
        <a  class="othermonth week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}" data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    } else {
        // 本月
        a += `<li class="">
        <a  class="currentmonth week_name_${calcTime(i).day}"  data-year="${calcTime(i).year}" data-month="${calcTime(i).month}" data-date="${calcTime(i).date}" data-time="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date}" title="${calcTime(i).year}-${calcTime(i).month}-${calcTime(i).date} ${lunarDatasShowtitle}">${calcTime(i).date}${lunarDatasShow}</a>
        </li>`;
    }
  }

  $$('weeklyCanlendarView').innerHTML = a;
};


  /* 设置禁用日期 */
  let setDisabled = function () {
      let arr = options.disabledDate || []; // 禁用日期数组
      let splitArr = null,
          arrYear = null,
          arrMonth = null,
          arrDate = null;
      let maxLen = 31;
      if (arr.length > maxLen) {
        throw new Error(`不可选日期天数限制${maxLen}天内`);
      }
      // 禁用的日期
      if (arr.length) {
        for (let index = 0; index < arr.length; index++) {
          splitArr = arr[index].split('-');
          arrYear = splitArr[0];
          arrMonth = splitArr[1];
          arrDate = splitArr[2];
          for (let j = 0; j < aTags.length; j++) {
            if ((arrYear === aTags[j].dataset.year) && (arrMonth === aTags[j].dataset.month) && (arrDate === aTags[j].dataset.date)) {
              aTags[j].classList.add('disabled');
            }
          }
        }
      }
  };

  /* 初始化日历 */
  let initDatetime = (function () {
    if (DateType == '3') { // 月历
       creatMonthCalendar(activeMonth, activeYear, 1);
       setMonthCalendarYearMonth(activeMonth, activeYear);
    } else { // 周历
       creatWeekCalendar(-activeDay, 1);
       setYearMonth(0);
    }
    setDisabled();
  }());

  // 设置初始点击次数
  $$('weeklyCanlendarView').setAttribute('clickedTimes', 0);
  // 获取点击次数
  let clickedTimes = $$('weeklyCanlendarView').getAttribute('clickedTimes');
  let weekNum = $$('week_selector').getAttribute('week');
  let initchangeWeek_curyear = activeYear;
  let initchangeWeek_curmonth = parseInt(activeMonth);
  /* 前一周/月和后一周/月方法 */
  let changeWeek = function (clickedTimes, weekNum, monthnum, yearnum) {
    if (DateType == '3') {
      // 类型月
      creatMonthCalendar(monthnum, yearnum, 0);
      setMonthCalendarYearMonth(monthnum, yearnum);
    } else {
       creatWeekCalendar(-activeDay - (weektype * clickedTimes), 0);
       setYearMonth(1);
       $$('weeklyCanlendarView').setAttribute('clickedTimes', clickedTimes);
    }
    setDisabled();
    let _d = new Date();
    let currDate = zeroize(_d.getDate()),
      currMonth = zeroize(_d.getMonth() + 1),
      currYear = parseInt(_d.getFullYear());
    options.clickDate && options.clickPreNextDate(selectedDateTime, DateType);
    // selectedDateTime===currYear+'-'+currMonth+'-'+currDate当前切换后的日期包含今天，则还需查询天日程，否则查询对应的月，周，双周日程
    // selectedDateTime===currYear+'-'+currMonth+'-'+currDate&&options.clickPreNextDate && options.clickPreNextDate(selectedDateTime,'1');
    options.getSelectedDate && options.getSelectedDate(selectedDateTime);
  };

  /* 前一周/月 */
  $$('prev_week').onclick = function (e) {
    clickedTimes++;
    weekNum--;
    initchangeWeek_curmonth - 1 < 1 ? (initchangeWeek_curmonth = 12, initchangeWeek_curyear -= 1) : (initchangeWeek_curmonth--, initchangeWeek_curyear = initchangeWeek_curyear);
    changeWeek(clickedTimes, weekNum, initchangeWeek_curmonth, initchangeWeek_curyear);
  };

  /* 后一周/月 */
  $$('next_week').onclick = function (e) {
    clickedTimes--;
    weekNum++;
    initchangeWeek_curmonth + 1 > 12 ? (initchangeWeek_curmonth = 1, initchangeWeek_curyear += 1) : (initchangeWeek_curmonth++, initchangeWeek_curyear = initchangeWeek_curyear);
    changeWeek(clickedTimes, weekNum, initchangeWeek_curmonth, initchangeWeek_curyear);
  };

  // /* 选择日期 */
  $$('weeklyCanlendarView').onclick = function (e) {
    let tagName = e.target.tagName.toLowerCase();
    if (tagName === 'a') {
      for (let i = 0, len = lis.length; i < len; i++) {
        lis[i].className = '';
        if ((lis[i].className.indexOf('clickActive') < 0) && lis[i].dataset.role === 'active') {
          lis[i].className = 'active';
        }
      }
      let _d = new Date();
      let currDate = zeroize(_d.getDate()),
        currMonth = zeroize(_d.getMonth() + 1),
        currYear = parseInt(_d.getFullYear());
      e.target.parentNode.className = 'clickActive';// 获取当前点击的日期
      selectedYear = e.target.getAttribute('data-year');// 当前点击年
      selectedMonth = e.target.getAttribute('data-month');// 当前点击月
      selectedDate = e.target.getAttribute('data-date');// 当前点击日
      if(selectedYear==currYear&&selectedMonth==currMonth&&selectedDate==currDate){
        e.target.parentNode.className = 'clickActive active';// 获取当前点击的日期
      }
      selectedDateTime = `${selectedYear}-${selectedMonth}-${selectedDate}`;
      options.clickDate && options.clickDate(selectedDateTime, '1');
      options.getSelectedDate && options.getSelectedDate(selectedDateTime);
    }
  };

  if (!selectedYear) {
    selectedDateTime = `${activeYear}-${activeMonth}-${activeDate}`;
    options.getSelectedDate && options.getSelectedDate(selectedDateTime);
  }
};

module.exports = {
  weeklyCalendar,
};
